<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('商户代付')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-merhcantWallet-withdrawal">
			<div class="form-group">
				<label class="col-sm-3 control-label">选择商户：</label>
				<div class="col-sm-8">
					<select class="form-control" id="merchantId" name="merchantId">
						<option value="" selected>----请选择----</option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">选择商户支付方法：</label>
				<div class="col-sm-8">
					<select class="form-control" id="mp_merchant_method_id" name="mp_merchant_method_id">
						<option value="" selected>----请选择----</option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">商户订单号：</label>
				<div class="col-sm-8">
					<input id="mch_code" name="mch_code" class="form-control" type="text" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">渠道订单号：</label>
				<div class="col-sm-8">
					<input id="ch_code" name="ch_code" class="form-control" type="text" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">支付金额(元)：</label>
				<div class="col-sm-8">
					<input id="total_fee" name="total_fee" class="form-control" type="text" required>
				</div>
			</div>
		</form>
	</div>
    <div th:include="include::footer"></div>
    <script type="text/javascript">
		var prefix = ctx + "transaction/transactionPay";
        var $form = $("#form-merhcantWallet-withdrawal");

        $.validator.addMethod("number",function (value, element, params) {
            var exp = new RegExp(params);
            return exp.test(value);
        },"请输入数字(注意空格)");
        $.validator.addMethod("regex",function (value, element, params) {
            var exp = new RegExp(params);
            return exp.test(value);
        },"请输入数字(可保留两位小数)");
        /*表单校验*/
        $form.validate({
			rules:{
                total_fee:{
                    regex:"^[0-9]+(.[0-9]{2}|.[0-9]{1})?$"
				},
                mch_code: {
                    number:"^[1-9]\\d*$"
				},
                ch_code: {
                    number:"^[1-9]\\d*$"
                }
			},
			focusCleanup: true
		});



        /*选择商户下拉框 */
        $.post(ctx + "merchant/merchantInfo/list", {},function (response) {
            var data = response.rows;
            var option = "";
            for (var i = 0; i < data.length; i++) {
                option += "<option value='"+ data[i].id +"'>" + data[i].name + "</option>"
            }
            $form.find("select[name='merchantId']").html("").append(option);
            // 初始化商户支付方法
            merchantMethod(data[0].id)
        });
        /* 商户下拉框下拉框选择事件   更新商户支付方法 */
        $form.find("select[name='merchantId']").change(function () {
            var merchantId = $form.find("select[name='merchantId']").val();
            // 更新商户支付方法
            merchantMethod(merchantId);
        });


        /*商户支付方法下拉框*/
		function merchantMethod(merchantId) {
            $.post(ctx + "merchant/mpMerchantMethod/list", {'merchantId':merchantId},function (response) {
                if (response.code == 0) {
                    var data = response.rows;
                    // var option = "<option value=''>" + "--------" + "</option>";
					var option = "";
                    for (var i = 0; i < data.length; i++) {
                        option += "<option value='"+ data[i].id +"'>" + data[i].channelName + " " + " " + " " +data[i].payKindName +"</option>"
                    }
                    $form.find("select[name='mp_merchant_method_id']").html("").append(option);
				}
			});
        }



        /*提交表单*/
        function submitHandler() {
            if ($.validate.form()) {
                var data = $form.serializeArray();
                // 转化成 (分)
                data[4].value = new Decimal(data[4].value).mul(new Decimal(100));
                $.operate.save(prefix + "/emptyOrder", data);
            }
        }
	</script>
</body>
</html>
